import adverService, { queryObject } from '../service/advertising'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus';


interface adverAction {
    getList: (callback: any) => void,
    getById: (id: any, callback: any) => void,
    addAdver: (obj: object) => void,
    updateAdver: (obj: object) => void,
    queryAdver: (obj: queryObject, callback: any) => void,
    deleteById: (id: any, obj: queryObject, callback: any) => void,
    test: () => void
}

export function adverHook(): adverAction {
    const router = useRouter()
    // console.log("router", router)

    // 获取广告列表
    function getList(callback: any) {
        adverService.getAdverList().then((res: any) => {
            if (res.data.code == 200 || res.data.code == 0) {
                console.log("请求信息", res.data)
                callback([...res.data.data], res.data.count)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("获取全部广告请求失败")
        })
    }

    // 查询广告
    function queryAdver(obj: queryObject, callback: any) {
        adverService.queryAdver(obj).then((res: any) => {
            console.log("请求信息", res.data)
            if (res.data.code == 200 || res.data.code == 0) {
                callback([...res.data.data], res.data.count)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("请求失败")
        })
    }

    // 根据id获取
    function getById(id: any, callback: any) {
        adverService.getAdverById(id).then((res: any) => {
            if (res.data.code == 200 || res.data.code == 0) {
                console.log("请求信息", res.data)
                callback(res.data.data)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("请求失败")
        })
    }

    // 添加广告
    function addAdver(obj: object) {
        adverService.addAdver(obj).then((res: any) => {
            console.log("响应信息", res.data)
            if (res.data.code == 200 || res.data.code == 0) {
                ElMessage({ type: "success", message: res.data.msg })

                router.push({ name: "advertising" })
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch((err: any) => {
            console.log("error", err)
            ElMessage("广告添加请求失败")
        })
    }


    // 修改广告
    function updateAdver(obj: object) {
        adverService.updateAdver(obj).then((res: any) => {
            if (res.data.code == 200 || res.data.code == 0) {
                // console.log("请求信息", res.data)
                ElMessage({ type: "success", message: res.data.msg })
                router.push({ name: "advertising" })
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch(() => {
            ElMessage("修改失败")
        })
    }

    // 根据id删除
    function deleteById(id: any, obj: queryObject, callback: any) {
        adverService.deleteById(id).then((res: any) => {
            console.log("响应信息", res.data)
            if (res.data.code == 200 || res.data.code == 0) {
                ElMessage({ type: "success", message: res.data.msg })
                queryAdver(obj, callback)
            } else {
                ElMessage.error(res.data.msg)
            }
        }).catch((err: any) => {
            console.log("error", err)
            ElMessage("删除失败")
        })
    }

    function test() {
        ElMessage("测试")
    }


    return {
        getList,
        getById,
        addAdver,
        updateAdver,
        queryAdver,
        deleteById,
        test
    }
}

